import Vue from 'vue'
import VueRouter from 'vue-router'
import Logon from '../components/logon.vue'
import List from '../components/list.vue'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'logon',
    component: Logon
  },
  {
    path: '/list',
    name: 'list',
    component: List
  }
]
const router = new VueRouter({
  routes
})

// 将导航守卫移至路由实例
router.beforeEach((to, from, next) => {
  if (to.path === '/list') {
    const token = localStorage.getItem('token')
    token ? next() : next('/')
  } else {
    next()
  }
})

export default router
